<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>register</title>
</head>

<body>
<div id="app">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="userName">
            <el-input v-model="ruleForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
            <el-select v-model="ruleForm.gender" placeholder="请选择">
                <el-option label="男" value="1"></el-option>
                <el-option label="女" value="2"></el-option>
                <el-option label="未知" value="3"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="账号" prop="account">
            <el-input v-model="ruleForm.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phoneNumber">
            <el-input v-model="ruleForm.phoneNumber"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</div>

<script src="js/vue.js"></script>
<script src="js/axios-0.18.0.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">

<script>
    new Vue({
        el: '#app',
        data() {
            return {
                ruleForm: {
                    userName: '',
                    gender: '',
                    account: '',
                    password: '',
                    phoneNumber: ''
                },
                rules: {
                    userName: [
                        {required: true, message: '请输入用户名称', trigger: 'blur'}
                    ],
                    gender: [
                        {required: true, message: '请选择性别', trigger: 'change'}
                    ],
                    account: [
                        {required: true, message: '请输入账号', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {
                            pattern: /^[a-zA-Z0-9]{8,16}$/,
                            message: '密码必须由8到16位的英文字母或数字组成',
                            trigger: 'blur'
                        }
                    ],
                    phoneNumber: [
                        {required: true, message: '请输入电话号码', trigger: 'blur'},
                        {
                            pattern: /^\d{11}$/,
                            message: '请输入正确的电话号码',
                            trigger: 'blur'
                        }
                    ],
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post('http://localhost:8080/register', {
                            userName:this.ruleForm.userName,
                            gender:this.ruleForm.gender,
                            account:this.ruleForm.account,
                            password:this.ruleForm.password,
                            phoneNumber:this.ruleForm.phoneNumber,
                        })
                            .then(response => {
                                if(response.msg == "userName"){
                                    this.$message.warning('用户不能为空');
                                }else if(response.msg =="gender"){
                                    this.$message.warning('性别不能为空');
                                }else if(response.msg =="account"){
                                    this.$message.warning('账号不能为空');
                                }else if(response.msg =="password"){
                                    this.$message.warning('密码不能为空');
                                }else if(response.msg =="phoneNumber"){
                                    this.$message.warning('电话号码不能为空');
                                }else if(response.msg =="phoneNumberLength"){
                                    this.$message.warning('请输入正确的电话号码');
                                }else{
                                    alert("注册成功");
                                    console.log('submit!!', this.ruleForm)
                                    window.location.href = 'login.html';
                                }
                            })
                            .catch(error => {
                                console.log(error);
                            })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    })
</script>
</body>
</html>